import { urlEncode } from '../../../utils/util.js'
import { Target, HttpUtil } from '../../../utils/http_util.js'
var config = require('../../../utils/config.js')
var utils = require('../../../utils/util.js')

Page({
  /* 页面的初始数据*/
  data: {
    locationInfo:'',  //坐标信息

    mid: 1,
    px: 30.5604132618, 
    py: 104.2705535889,
    markers: [{
      id: 0,
      latitude: 30.5604132618,
      longitude: 104.2705535889,
      title: "小妹妹",
      callout: { content: "技师小妹妹的信息！！" }

    }],
    inpoints: [],

    formData:{
      businessTypeId:'',
      isOnline:'',
      sex:'',
      distance:10
    },

    // 筛选导航栏数据
    msgList: [
      { key: 1, name: '分类',cid:0 },
      { key: 2, name: '性别', cid: 0 },
      { key: 3, name: '状态', cid: 0 },
      { key: 4, name: '10KM', cid: 1 }
    ],

    // 判断导航栏列表是否显示
    meunShow: [
      { isShows: true },
      { isShows: true },
      { isShows: true },
      { isShows: true }
    ],
    classList: [
      { id: 99, name: '不限', cid: 0},
      { id: 1, name: '足疗',cid:0 },
      { id: 2, name: '推拿', cid: 0},
      { id: 3, name: '美容护肤', cid: 0},
      { id: 4, name: '美妆', cid: 0},
      { id: 5, name: '美甲', cid: 0},
      { id: 6, name: '拔罐', cid: 0},
      { id: 7, name: '体型', cid: 0}
    ],
    sexlist: [
      { id: 99, name: '不限', cid: 0},
      { id:0, name: '女', cid: 0},
      { id: 1, name: '男', cid: 0}
    ],
    statelist: [
      { id: 99, name: '不限' ,cid: 0},
      { id: 1, name: '在线' ,cid: 0},
      { id: 0, name: '离线' ,cid: 0}
    ],
    distancelist:[
      { id: 2, name: '2KM', cid: 0 },
      { id: 5, name: '5KM', cid: 0 },
      { id: 10, name: '10KM', cid: 1 },
      { id: 20, name: '20KM', cid: 0 },
      { id: 30, name: '30KM', cid: 0 },
      { id: 50, name: '50KM', cid: 0 }
    ]
  },
  onLoad: function (options) {
    var that =this;
    wx.getStorage({
      key: 'locationInfo',
      success(lores) {
        that.setData({
          locationInfo: lores.data
        });
        that.loadData();
      }
    })  
  },

  // 筛选导航栏事件
  menuClick: function (e) {
    // 获取通过wxml  data-hi="{{ idx }}" 传过来的索引
    var menuNum = e.currentTarget.dataset.hi;

    // 拼接 ，使我们可以获取到menuShow里面每一个isSHows
    var menuSrc = "meunShow[" + menuNum + "].isShows";

    // 循环data中设置的meunShow
    for (var n = 0; n < this.data.meunShow.length; n++) {
      // 拼接 ，使我们可以获取到menuShow里面每一个isSHows
      var menuSrcs = "meunShow[" + n + "].isShows";
      // 解决重复点击不能隐藏的问题
      if (n != menuNum) {
        // 初始化，每次点击时先全部隐藏，但是重复点击不会隐藏
        this.setData({
          [menuSrcs]: true
        });
      };
    };

    // 给当前点击的去反data中设置的meunShow，使之显示， 只写此处只会显示不能隐藏
    this.setData({
      [menuSrc]: !this.data.meunShow[e.currentTarget.dataset.hi].isShows
    });
  },



  //搜索入口  
  // wxSearchTab: function () {
  //   var that = this;
  //   wx.redirectTo({
  //     url: '/pages/mapsearch/search'
  //   })
  // },
  checkClass:function(event){
    var that = this;
    var check = event.currentTarget.dataset;
    var relist = that.data.classList
    var reslist =[];
    for(var i= 0 ;i<relist.length;i++){
      if (check.id == relist[i].id){
        var checkobj = relist[i];
        checkobj.cid=1;
        reslist.push(checkobj);
      }else{
        var checkobj = relist[i];
        checkobj.cid = 0;
        reslist.push(checkobj);
      }
    }
    that.setData({
      classList: reslist
    })
  },
  checkcancel:function(){
    var that = this;
    that.setData({
        "meunShow[0].isShows": true
      });
  },
  checkcomfire:function(){
    var that = this;
    var classList = that.data.classList;
    for(var i = 0;i<classList.length;i++){
      if (classList[i].cid == 1 && classList[i].id!=99){
        that.setData({
          "formData.businessTypeId": classList[i].id,
          "msgList[0].name": classList[i].name,
          "msgList[0].cid": 1
        })
      } else if (classList[i].cid == 1 && classList[i].id == 99){
        that.setData({
          "formData.businessTypeId": '',
          "msgList[0].name": "分类",
          "msgList[0].cid": 0
        })
      }
    }
    that.setData({
      "meunShow[0].isShows": true
    })
    that.loadData();
  },

  //性别选择
  checkSex: function (event) {
    var that = this;
    var check = event.currentTarget.dataset;
    var relist = that.data.sexlist
    var reslist = [];
    for (var i = 0; i < relist.length; i++) {
      if (check.id == relist[i].id) {
        var checkobj = relist[i];
        checkobj.cid = 1;
        reslist.push(checkobj);
      } else {
        var checkobj = relist[i];
        checkobj.cid = 0;
        reslist.push(checkobj);
      }
    }
    that.setData({
      sexlist: reslist
    })
  },
  checkSexcancel: function () {
    var that = this;
    that.setData({
      "meunShow[1].isShows": true
    });
  },
  checkSexcomfire: function () {
    var that = this;
    var sexList = that.data.sexlist;
    for (var i = 0; i < sexList.length; i++) {
      if (sexList[i].cid == 1 && sexList[i].id != 99) {
        that.setData({
          "formData.sex": sexList[i].id,
          "msgList[1].name": sexList[i].name,
          "msgList[1].cid": 1
        })
        
      } else if (sexList[i].cid == 1 && sexList[i].id == 99) {
        that.setData({
          "formData.sex": '',
          "msgList[1].name": "性别",
          "msgList[1].cid": 0
        })
      }
    }
    that.setData({
      "meunShow[1].isShows": true
    })
    that.loadData();
  },


  //状态选择
  checkState: function (event) {
    var that = this;
    var check = event.currentTarget.dataset;
    var relist = that.data.statelist
    var reslist = [];
    for (var i = 0; i < relist.length; i++) {
      if (check.id == relist[i].id) {
        var checkobj = relist[i];
        checkobj.cid = 1;
        reslist.push(checkobj);
      } else {
        var checkobj = relist[i];
        checkobj.cid = 0;
        reslist.push(checkobj);
      }
    }
    that.setData({
      statelist: reslist
    })
  },
  checkStatecancel: function () {
    var that = this;
    that.setData({
      "meunShow[2].isShows": true
    });
  },
  checkStatecomfire: function () {
    var that = this;
    var statelist = that.data.statelist;
    for (var i = 0; i < statelist.length; i++) {
      if (statelist[i].cid == 1 && statelist[i].id != 99) {
        that.setData({
          "formData.isOnline": statelist[i].id,
          "msgList[2].name": statelist[i].name,
          "msgList[2].cid": 1
        })
        
      } else if (statelist[i].cid == 1 && statelist[i].id == 99) {
        that.setData({
          "formData.isOnline": '',
          "msgList[2].name": "状态",
          "msgList[2].cid": 0
        })
      }
    }
    that.setData({
      "meunShow[2].isShows": true
    })
    that.loadData();
  },

  //距离选择
  checkDistance: function (event) {
    var that = this;
    var check = event.currentTarget.dataset;
    var relist = that.data.distancelist
    var reslist = [];
    for (var i = 0; i < relist.length; i++) {
      if (check.id == relist[i].id) {
        var checkobj = relist[i];
        checkobj.cid = 1;
        reslist.push(checkobj);
      } else {
        var checkobj = relist[i];
        checkobj.cid = 0;
        reslist.push(checkobj);
      }
    }
    that.setData({
      distancelist: reslist
    })
  },
  checkDistancecancel: function () {
    var that = this;
    that.setData({
      "meunShow[3].isShows": true
    });
  },
  checkDistancecomfire: function () {
    var that = this;
    var distancelist = that.data.distancelist;
    for (var i = 0; i < distancelist.length; i++) {
      if (distancelist[i].cid == 1 ) {
        that.setData({
          "formData.distance": distancelist[i].id,
          "msgList[3].name": distancelist[i].name,
          "msgList[3].cid": 1
        })
      }
    }
    that.setData({
      "meunShow[3].isShows": true
    })
    that.loadData();
  },

  loadData:function(){
    var that = this;
    var param={
      viewProfessionalTitle: that.data.formData.businessTypeId,
      evaluation:false,
      consumerLongitude: that.data.locationInfo.longitude,
      consumerLatitude: that.data.locationInfo.latitude,
      sex: that.data.formData.sex,
      enabled: that.data.formData.isOnline,
      distance: that.data.formData.distance
    }
    HttpUtil.get(Target.self, "artificerMeans/orderList", param).then(res => {
      console.log(res);
      var datlist = res.data;
      var inpoints = [];
      for(var i =0;i<datlist.length;i++){
        // var ary = wx.base64ToArrayBuffer(datlist[i].base64)
        // var base64 = wx.arrayBufferToBase64(ary)
        var point ={
          id: datlist[i].id,
          latitude: datlist[i].latitude, 
          longitude: datlist[i].longitude, 
          iconPath: config.ictx + datlist[i].imgUrl + config.txCon
          //iconPath: 'data:image/jpg;base64,' + wx.getFileSystemManager().readFileSync("/images/menu_zuliao.png", 'base64') 
          //iconPath: 'data:image/jpg;base64,' + base64
        };
        inpoints.push(point);
      }
      that.setData({
        inpoints: inpoints,
        markers: inpoints
      })
    })


    
  }
})